package com.springapp.mvc.dm;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by shangdong on 17/4/9.
 */
public class EnterMain {

    public static ExecutorService executorService = Executors.newFixedThreadPool(30);

    private static DmRunService dmRunService = new DmRunService();


    public static void main(String[] args) {

//        newRunForTest();
        if (args.length == 0){
            System.out.println("请填写用户列表路径及产品列表路径，以空格分割！");
            return;
        }
        if (StringUtils.isBlank(args[0]) || StringUtils.isBlank(args[1])){
            System.out.println("请填写用户列表路径及产品列表路径，以空格分割！");
            return;
        }
        try {
            FileOutPut fileOutPut = new FileOutPut(args).invoke();
            List<User> userList = fileOutPut.getUserList();
            List<Product> productList = fileOutPut.getProductList();
            System.out.println(userList);
            System.out.println(productList);
//            System.out.println(userList);
//            oldRun(userList, productList);
            dmRunService.setCountListener(new NewCountListenerImpl(userList));
            dmRunService.queryProduct(productList,2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void newRunForTest(){
        try {
//            FileOutPut fileOutPut = new FileOutPut(args).invoke();
//            List<User> userList = fileOutPut.getUserList();
//            List<Product> productList = fileOutPut.getProductList();
//            System.out.println(userList);
//            oldRun(userList, productList);
            dmRunService.setCountListener(new NewCountListenerImpl(User.getAllUsers()));
            dmRunService.queryProductForTest(Product.getAllProduct(),3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    private static void oldRun(List<User> userList, final List<Product> productList) {
//        for (User user : userList) {
//            System.out.println(user);
//            executorService.submit(new Runnable() {
//                @Override
//                public void run() {
//                    DmConnClient.executeNewUser(user, productList);
//                }
//            });
//        }
//    }

    public void start(){
        for (User user : User.getAllUsers()) {
            System.out.println(user);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    DmConnClient.executeNewUser(user, Product.getAllProduct());
                }
            });
        }
    }

    private static class FileOutPut {
        private String[] args;
        private List<User> userList;
        private List<Product> productList;

        public FileOutPut(String... args) {
            this.args = args;
        }

        public List<User> getUserList() {
            return userList;
        }

        public List<Product> getProductList() {
            return productList;
        }

        public FileOutPut invoke() throws IOException {
            File users = new File(args[0]);//"/Users/shangdong/IdeaProjects/dmkiller/out/artifacts/dmkiller_jar/user.txt"
            File products = new File(args[1]);//"/Users/shangdong/IdeaProjects/dmkiller/out/artifacts/dmkiller_jar/product.txt"
            List<String> userString = Files.readLines(users, Charsets.UTF_8);
            List<String> productString = Files.readLines(products, Charsets.UTF_8);
            userString.remove(0);
            productString.remove(0);
            userList = Lists.transform(userString, new Function<String, User>() {
                @Override
                public User apply(String input) {
                    return User.parseUser(input);
                }
            });
            productList = Lists.transform(productString, new Function<String, Product>() {
                @Override
                public Product apply(String input) {
                    return Product.parseProduct(input);
                }
            });
            return this;
        }
    }
}
